﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for NewsDAL
/// </summary>
public class NewsDAL : BaseDAL
{
    public DataSet List(int pageIndex, int pageMax, int groupNewsId, int isActive)
    {
        DataSet ds = new DataSet();
        //string[] uspParaName = new string[] { "?pageIndex", "?pageMax", "?groupNewsId", "?isActive" };
        //object[] uspParaValue = new object[] { pageIndex, pageMax, groupNewsId, isActive };
        //string uspName = "usp_news_list";

        //ds = base.RunProcedure(uspParaName, uspParaValue, uspName);

        string query = string.Format("select * from [dbo].[News] Id = {0} ", groupNewsId.ToString());
        ds = base.RunQuery(query);

        return ds;
    }
    public DataSet Detail(int newsId)
    {
        DataSet ds = new DataSet();

        string query = "select * from News where Id = " + newsId.ToString();
        ds = base.RunQuery(query);

        return ds;
    }
    public DataSet UpdateStatusActive(int newsid, int IsActive)
    {
        DataSet ds = new DataSet();
        string query = string.Format("Update News set IsActive={0} where id={1}; Select {0} as Id", IsActive, newsid);
        ds = base.RunQuery(query);
        return ds;
    }
    public DataSet Insert(string title, string thumbnail, string content, string description, int userid, int groupNewsId, int status, int isActive)
    {
        DataSet ds = new DataSet();
        string query = string.Format("insert into news (title, thumbnail, content, description, CreatedTime, createdUserId, GroupNewsId, ModifiedTime, Status, IsActive)" +
            " values ('{0}','{1}','{2}','{3}','{4}',{5},{6},'{4}',{7}, {8});" +
            "Select @@identity as Id;",
            title, thumbnail, content, description, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), userid, groupNewsId, status, isActive);
        ds = base.RunQuery(query);
        return ds;
    }
    public DataSet Update(int newsid, string title, string thumbnail, string content, string description, int groupNewsId, int status, int isActive)
    {
        DataSet ds = new DataSet();
        string query = string.Format("Update news set Title='{0}', Thumbnail='{1}', Content='{2}', Description='{3}'," + 
            " GroupNewsId={4}, ModifiedTime='{5}', Status={6}, IsActive={8} " + 
            "WHERE Id={7}; Select {7} as Id",
            title, thumbnail, content, description, groupNewsId, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), status, newsid, isActive);
        ds = base.RunQuery(query);
        return ds;
    }
}